﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SRW;
using SRW.BLL;
using System.Text;
using System.Collections.Generic;

public partial class system_xtgl_news : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            AspNetPager1.RecordCount = SQL.ExecScalar("select count(*) from wtnews");
            AspNetPager1.PageSize = 15;

            PageData.bindData(Repeater1, "select * from wtnews order by pkID desc", "wtnews", AspNetPager1);

        }
    }
    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        PageData.bindData(Repeater1, "select * from wtnews order by pkID desc", "wtnews", AspNetPager1);
    }
    public void setImage(FileUpload FileUp)
    {
        string str = FileOper.FileUp(FileUp, "../../LoadImage/");
        if (str == "上传成功")
        {
            HttpPostedFile httpup = FileUp.PostedFile;

            string PathFileName = httpup.FileName;
            int i = PathFileName.LastIndexOf("\\");
            string filename = PathFileName.Substring(i + 1);
            Newsimage mNewsimage = new Newsimage();
            mNewsimage.imaplace = "../LoadImage/" + filename;
            mNewsimage.newsID = SQL.GetNewId("wtnews", "pkID");
            mNewsimage.newsname = bWtnews.select(mNewsimage.newsID).wtnewsName;
            bNewsimage.Insert(mNewsimage, false);  
        }
    }
    public void setFile(FileUpload FileUp)
    {
        string str = FileOper.FileUp(FileUp, "../../LoadFile/");
        if (str == "上传成功")
        {
            HttpPostedFile httpup = FileUp.PostedFile;
            string PathFileName = httpup.FileName;
            int i = PathFileName.LastIndexOf("\\");
            string FileName = PathFileName.Substring(i + 1);
            string FilePlace = "../LoadFile/" + FileName;
            int newID = SQL.GetNewId("wtnews", "pkID");
            StringBuilder mSbuilder = new StringBuilder();
            mSbuilder.Append("insert into nFileType(FilePlace,newID,FileName) values(");
            mSbuilder.Append("'" + FilePlace + "'," + newID + ",'" + FileName + "')");
            SQL.ExecScalarSQL(mSbuilder.ToString());
        }
    }
    public void setLoadType(FileUpload FileUp)
    {
        HttpPostedFile httpup = FileUp.PostedFile;

        string PathFileName = httpup.FileName;
        int i = PathFileName.LastIndexOf(".");
        string filetype = PathFileName.Substring(i + 1);

        if (filetype == "jpg" || filetype == "gif" || filetype == "jpeg" || filetype == "png")
        {
            setImage(FileUp);
        }
        else
        {
            setFile(FileUp);
        }
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        //发表新闻
        if (Session["username"]==null)
        {
            Anthem.Manager.AddScriptForClientSideEval("alert('请重新登录后再进行发表！')");
            return;
        }
        if (DropDownList1.SelectedItem.Text == "请选择新闻类型")
        {
            Anthem.Manager.AddScriptForClientSideEval("alert('请选择要发表的新闻类型！')");
            return;
        }
        if (string.IsNullOrEmpty(TextBox1.Text.Trim()) || string.IsNullOrEmpty(Editor1.Text.Trim()))
        {
            Anthem.Manager.AddScriptForClientSideEval("alert('请填写新闻标题或者内容！')");
            return;
        }
        Wtnews mWtnews = new Wtnews();
        mWtnews.wtnewsContent = Editor1.Text;
        mWtnews.wtnewsDate =DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
        mWtnews.wtnewsMan = Session["username"].ToString();
        mWtnews.wtnewsName = TextBox1.Text.Trim();
        mWtnews.wtnewsType = DropDownList1.SelectedItem.Text;
        if (bWtnews.Insert(mWtnews, false))
        {
           // MagicAjax.AjaxCallHelper.WriteAlert("发表成功！");
            if (FileUpload1.PostedFile.ContentLength != 0)
            {
                setLoadType(FileUpload1);
            }
            if (FileUpload2.PostedFile.ContentLength != 0)
            {
                setLoadType(FileUpload2);
            }
            if (FileUpload3.PostedFile.ContentLength != 0)
            {
                setLoadType(FileUpload3);
            }
            if (FileUpload4.PostedFile.ContentLength != 0)
            {
                setLoadType(FileUpload4);
            }
            if (FileUpload5.PostedFile.ContentLength != 0)
            {
                setLoadType(FileUpload5);
            }

            PageData.bindData(Repeater1, "select * from wtnews order by pkID desc", "wtnews", AspNetPager1);
        }
        else
        {
            Anthem.Manager.AddScriptForClientSideEval("alert('系统出现错误，可联系技术人员！')");
        }
    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        //全选
        CheckBox2.Checked = true;
        CheckBox3.Checked = false;
        foreach (RepeaterItem item in this.Repeater1.Items)
        {
            CheckBox CheckBox1 = (CheckBox)item.FindControl("CheckBox1");
            if (!CheckBox1.Checked)
            {
                CheckBox1.Checked = true;
            }
        }
    }
    protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
    {
        //反选
        CheckBox3.Checked = true;
        CheckBox2.Checked = false;
        foreach (RepeaterItem item in this.Repeater1.Items)
        {
            CheckBox CheckBox1 = (CheckBox)item.FindControl("CheckBox1");
            if (CheckBox1.Checked)
            {
                CheckBox1.Checked = false;
            }
            else
            {
                CheckBox1.Checked = true;
            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
         //删除
        bool panduan = false;
        foreach (RepeaterItem item in this.Repeater1.Items)
        {
            CheckBox CheckBox1 = (CheckBox)item.FindControl("CheckBox1");
            if (CheckBox1.Checked)
            {
                Label Label1 = (Label)item.FindControl("Label1");
                int pkID = int.Parse(Label1.Text);
                List<string> SQLStringList = new List<string>();
                SQLStringList.Add("delete from wtnews where pkID="+pkID+"");
                SQLStringList.Add("delete from newsimage where newsID="+pkID+"");
                if (SQL.ExecuteSqlTran(SQLStringList))
                {
                    panduan = true;
                }
            }
        }
        if (!panduan)
        {
            MagicAjax.AjaxCallHelper.WriteAlert("删除成功！");
            PageData.bindData(Repeater1, "select * from wtnews order by pkID desc", "wtnews", AspNetPager1);
        }
        else
        {
            MagicAjax.AjaxCallHelper.WriteAlert("系统出现错误！");
        }
    }
}
